﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace FourKill.Engine
{
    public class AnotherTree : IPaintable
    {
        private static AnotherTree _instance;
        private const int TREE_TRIANGLES = 4;
        public static AnotherTree Instance { get { _instance = _instance ?? new AnotherTree(); return _instance; } }

        public void Paint(IPaintableContext context)
        {
            if (context.GameObject.LocationRelativeToCamera == null)
                context.GameObject.UpdateLocation(context);
            var location = context.GameObject.LocationRelativeToCamera;

            if (location.IsVisible)
            {
                try
                {
                    using (SolidBrush sb = new SolidBrush(context.GameObject.Color))
                    {
                        context.FrontLayer.SmoothingMode = SmoothingMode.AntiAlias;

                        for (int i = 0; i < TREE_TRIANGLES; i++)
                        {
                            Point p1 = new Point(location.ObjectRectangle.X + location.ObjectRectangle.Width / 2, location.ObjectRectangle.Y + (i * location.ObjectRectangle.Height / TREE_TRIANGLES) - location.ObjectRectangle.Height / 4);
                            Point p2 = new Point(location.ObjectRectangle.X, location.ObjectRectangle.Y + (i * location.ObjectRectangle.Height / TREE_TRIANGLES) + location.ObjectRectangle.Height / TREE_TRIANGLES);
                            Point p3 = new Point(location.ObjectRectangle.X + location.ObjectRectangle.Width, location.ObjectRectangle.Y + (i * location.ObjectRectangle.Height / TREE_TRIANGLES) + location.ObjectRectangle.Height / TREE_TRIANGLES);
                            context.FrontLayer.FillPolygon(sb, new Point[] { p1, p2, p3 });
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("***********************************");
                    System.Diagnostics.Debug.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
                    System.Diagnostics.Debugger.Break();
                }
            }
        }
    }
}
